package com.zw.cloud.mybatis.plus.service.impl.stenter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zw.cloud.mybatis.plus.entity.stenter.OptimizeCraft;
import com.zw.cloud.mybatis.plus.entity.stenter.OptimizeCraftDTO;
import com.zw.cloud.mybatis.plus.entity.stenter.OptimizeCraftPageDTO;
import com.zw.cloud.mybatis.plus.entity.stenter.OptimizeCraftPageQueryDTO;
import com.zw.cloud.mybatis.plus.mapper.stenter.OptimizeCraftMapper;
import com.zw.cloud.mybatis.plus.service.api.stenter.IOptimizeCraftService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zw.cloud.mybatis.plus.service.api.stenter.IProductRecordDetailService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 定型可优化工艺 服务实现类
 * </p>
 *
 * @author zw
 * @since 2024-01-17
 */
@Service
public class OptimizeCraftServiceImpl extends ServiceImpl<OptimizeCraftMapper, OptimizeCraft> implements IOptimizeCraftService {

    @Autowired
    private IProductRecordDetailService productRecordDetailService;
    @Override
    public Page<OptimizeCraft> pageQueryOptimizeCraft(OptimizeCraftPageQueryDTO pageQueryDTO) {
        LambdaQueryWrapper<OptimizeCraft> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OptimizeCraft::getOrgCode,pageQueryDTO.getOrgCode());
        queryWrapper.orderByDesc(OptimizeCraft::getId);
        Page<OptimizeCraft> page = new Page<>(pageQueryDTO.getPageNo(),pageQueryDTO.getPageSize());
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public OptimizeCraftDTO queryOptimizeCraftById(Long id) {
        OptimizeCraft optimizeCraft = baseMapper.selectById(id);
        if (Objects.isNull(optimizeCraft)) {
            return null;
        }
        OptimizeCraftDTO optimizeCraftDTO = new OptimizeCraftDTO();
        BeanUtils.copyProperties(optimizeCraft,optimizeCraftDTO);

        Object contrastProductRecordId = optimizeCraft.getContrastProductRecordId();
        Map<String, List<Long>> contrastProductRecordIdMap = JSON.parseObject(JSON.toJSONString(contrastProductRecordId), new TypeReference<HashMap<String, List<Long>>>() {});
        List<Long> byIdList = contrastProductRecordIdMap.get("byId");
        List<Long> byMatchList = contrastProductRecordIdMap.get("byMatch");
        if (CollectionUtils.isNotEmpty(byIdList)) {
            List<OptimizeCraftPageDTO> optimizeCraftPageDTOList = productRecordDetailService.queryOptimizeCraftDataByProductRecordId(byIdList);
            optimizeCraftDTO.setByIdList(optimizeCraftPageDTOList);
        }
        if (CollectionUtils.isNotEmpty(byMatchList)) {
            List<OptimizeCraftPageDTO> optimizeCraftPageDTOList = productRecordDetailService.queryOptimizeCraftDataByProductRecordId(byMatchList);
            optimizeCraftDTO.setByMatchList(optimizeCraftPageDTOList);
        }
        return optimizeCraftDTO;
    }
}
